//
//  DUDayView.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/10/20.
//

import Cocoa

@IBDesignable
class DUDayView: BaseView {

    private var holiday:NSTextField!    //加班、休息显示控件
    private var solarDay:NSTextField!   //公历显示控件
    private var lunarDay:NSTextField!   //农历或节假日显示控件
    private var backDay:NSTextField!    //背景显示，多用于加班、休息、非当月数据等
    
    var date:DateEntity?
    private var style:DayColorModel?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    convenience init() {
        self.init(frame: CGRect.zero)
    }
    
    override func layout() {
        super.layout()
        backDay.layer?.cornerRadius = 8
        self.layer?.cornerRadius = 8
        //backDay.backgroundColor = style?.maskColor
        //backDay.isHidden = style == nil ? true: style!.maskHidden
    }
    
    private func initialize() {
        self.setFrameSize(NSSize(width: 50, height: 50))
        holiday = DULabel()
        holiday.frame = NSRect(x: 30, y: 30, width: 15, height: 15)
        holiday.font = NSFont.systemFont(ofSize: 8)
        holiday.stringValue = ""
        self.addSubview(holiday)
        solarDay = DULabel()
        solarDay.frame = NSRect(x: 0, y: 11, width: 50, height: 29)
        solarDay.font = NSFont.systemFont(ofSize: 24)
        solarDay.textColor = NSColor.textColor
        solarDay.stringValue = "28"
        self.addSubview(solarDay)
        lunarDay = DULabel()
        lunarDay.frame = NSRect(x: 0, y: 2, width: 50, height: 13)
        lunarDay.font = NSFont.systemFont(ofSize: 10)
        lunarDay.textColor = NSColor.textColor
        
        lunarDay.stringValue = "廿八"
        self.addSubview(lunarDay)
        backDay = DULabel()
        backDay.frame = NSRect(x: 0, y: 0, width: 50, height: 50)
        backDay.stringValue = ""
        backDay.drawsBackground = true
        self.addSubview(backDay)
        backDay.isHidden = false
    }
    
    func setDefaultDay(date:DateEntity) {
        self.date = date
        holiday.stringValue = ""
        solarDay.stringValue = String(date.solar.day)
        lunarDay.stringValue = date.lunar.dayToMonthStr
        backDay.isHidden = true
    }
    
    func isWeekend() -> Bool {
        if date?.solar.week == 1 || date?.solar.week == 7 {
            return true
        }
        return false
    }
    
    func setStyle(style:DayColorModel) {
        self.style = style
        solarDay.textColor = style.solarForegroundColor
        lunarDay.textColor = style.lunarForegroundColor
        backDay.backgroundColor = style.maskColor
        backDay.isHidden = style.maskIsHidden
    }

    @IBInspectable var holidayString:String = "" {
        didSet {
            self.holiday.stringValue = holidayString
        }        
    }
    
}
